package com.xtwy.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.GroupContext;
import org.tio.core.intf.Packet;
import org.tio.core.stat.IpStat;
import org.tio.core.stat.IpStatListener;
import org.tio.utils.json.Json;

/**
 * @描述：
 * ip状态监听器
 *
 * @公司：  山西迅腾伟业
 * @版本：  1.0.0
 * @日期：  18/08/08
 * @作者：  侯建军
 */
public class RXIpStatListener implements IpStatListener{
    private static Logger log = LoggerFactory.getLogger(RXIpStatListener.class);
    //创建一个单例对象
    public static final RXIpStatListener me = new RXIpStatListener();

    /**
     * 失效时状态
     * @param groupContext
     * @param ipStat
     */
    @Override
    public void onExpired(GroupContext groupContext, IpStat ipStat) {
        //在这里把统计数据写入数据库中或日志中
        if (log.isInfoEnabled()) {
            log.info("IP状态\r\n{}", Json.toFormatedJson(ipStat));
        }
    }

    /**
     *  连接后状态
     * @param channelContext
     * @param isConnected
     * @param isReconnect
     * @param ipStat
     * @throws Exception
     */
    @Override
    public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect, IpStat ipStat) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("连接后\r\n{}", Json.toFormatedJson(ipStat));
        }
    }

    /**
     * 数据解码错误
     * @param channelContext
     * @param ipStat
     */
    @Override
    public void onDecodeError(ChannelContext channelContext, IpStat ipStat) {
        if (log.isInfoEnabled()) {
            log.info("数据解码错误\r\n{}", Json.toFormatedJson(ipStat));
        }
    }

    /**
     *  消息发送后
     * @param channelContext
     * @param packet
     * @param isSentSuccess
     * @param ipStat
     * @throws Exception
     */
    @Override
    public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess, IpStat ipStat) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("消息发送后\r\n{}\r\n{}", packet.logstr(), Json.toFormatedJson(ipStat));
        }
    }

    /**
     * 数据解码后
     * @param channelContext
     * @param packet
     * @param packetSize
     * @param ipStat
     * @throws Exception
     */
    @Override
    public void onAfterDecoded(ChannelContext channelContext, Packet packet, int packetSize, IpStat ipStat) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("数据解码后\r\n{}\r\n{}", packet.logstr(), Json.toFormatedJson(ipStat));
        }
    }

    /**
     * 数据接受后
     * @param channelContext
     * @param receivedBytes
     * @param ipStat
     * @throws Exception
     */
    @Override
    public void onAfterReceivedBytes(ChannelContext channelContext, int receivedBytes, IpStat ipStat) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("数据接受后\r\n{}", Json.toFormatedJson(ipStat));
        }
    }

    /**
     * 数据处理后
     * @param channelContext
     * @param packet
     * @param ipStat
     * @param cost
     * @throws Exception
     */
    @Override
    public void onAfterHandled(ChannelContext channelContext, Packet packet, IpStat ipStat, long cost) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("数据处理后\r\n{}\r\n{}", packet.logstr(), Json.toFormatedJson(ipStat));
        }
    }
}

